package suministros.ejb30.DAO;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.mail.MessagingException;
import javax.naming.NamingException;
import javax.annotation.Resource;
import java.util.Map;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.ejb.Stateless;
import javax.mail.util.ByteArrayDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author Renato
 */
@Stateless
public class ReporteGenera implements ReporteGeneraLocal {
    @EJB
    private SumiEMailLocal sumiEMail;

    @Resource (name="jdbc/Administracion")
    private javax.sql.DataSource ds;

     public JasperPrint generarReporte(String reporte, Map parametros) throws SQLException, JRException {
        Connection cn = ds.getConnection();
        parametros.put("REPORT_LOCALE", java.util.Locale.ENGLISH);
        parametros.put("SUBREPORT_DIR",getClass().getResource("/suministrosReportes/").getPath());
        JasperReport report = (JasperReport) JRLoader.loadObject(getClass().getResource("/suministrosReportes/" + reporte));
        JasperPrint jasperPrint = JasperFillManager.fillReport( report, parametros, cn );
        cn.close();
        return jasperPrint;

    }

    public void generarReporteAEmail(JasperPrint print, Map parametros) throws SQLException, JRException {
           
        DataHandler dataHandler=null;
        JRPdfExporter pdfexporter=new  JRPdfExporter();
        // String destFileNamePdf="C:\\322332.pdf";
        //pdfexporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFileNamePdf);
        //Creación del PDF
        //   JasperExportManager.exportReportToPdfFile(print, destFileNamePdf);
        ByteArrayOutputStream is = new ByteArrayOutputStream();
        pdfexporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
        pdfexporter.setParameter(JRExporterParameter.OUTPUT_STREAM, is);
        pdfexporter.exportReport();
        DataSource dataSource = new ByteArrayDataSource(is.toByteArray(),"application/pdf");
        dataHandler = new DataHandler(dataSource);
        try {
            //this.sumiEMail.enviarEmailComplejo("renato.castillo@epsgrau.com.pe", "orden", "orden de compra Nº", "322332.pdf", destFileNamePdf);
            this.sumiEMail.enviarEmailComplejo(parametros.get("email").toString(), parametros.get("asunto").toString(),parametros.get("mensaje").toString() ,parametros.get("cod_orden")+ ".pdf", dataHandler);
        } catch (NamingException ex) {
            Logger.getLogger(ReporteGenera.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MessagingException ex) {
            Logger.getLogger(ReporteGenera.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    }


     public JasperPrint generarReporteIngreso(String reporte, Map parametros) throws SQLException, JRException {
        Connection cn = ds.getConnection();
        parametros.put("REPORT_LOCALE", java.util.Locale.ENGLISH);
        parametros.put("SUBREPORT_DIR",getClass().getResource("/suministrosReportes/").getPath());
        JasperReport report = (JasperReport) JRLoader.loadObject(getClass().getResource("/suministrosReportes/" + reporte));
        JasperPrint jasperPrint = JasperFillManager.fillReport( report, parametros, cn );
        cn.close();
        return jasperPrint;
    }
     public JasperPrint generarReporteSalida(String reporte, Map parametros) throws SQLException, JRException {
        Connection cn = ds.getConnection();
        parametros.put("REPORT_LOCALE", java.util.Locale.ENGLISH);
        parametros.put("SUBREPORT_DIR",getClass().getResource("/suministrosReportes/").getPath());
        JasperReport report = (JasperReport) JRLoader.loadObject(getClass().getResource("/suministrosReportes/" + reporte));
        JasperPrint jasperPrint = JasperFillManager.fillReport( report, parametros, cn );
        cn.close();
        return jasperPrint;
    }



}
